﻿<UserControl x:Class="Studio.DocumentDB.AccountListControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:local="clr-namespace:Studio.DocumentDB"
             xmlns:model="clr-namespace:Model.DocumentDB;assembly=Model.DocumentDB"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.Resources>
        <!--  Style for a label with an image next to it  -->
        <Style x:Key="imageLabel" TargetType="Label">
            <Setter Property="Margin" Value="2"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
        </Style>
    </UserControl.Resources>
    <DockPanel LastChildFill="True">
        <local:WaitingUserControl DockPanel.Dock="Top">
            <local:WaitingUserControl.Style>
                <!--  Style toggling a visibility on an element depending on the accounts are loading or not  -->
                <Style TargetType="FrameworkElement">
                    <Setter Property="Visibility" Value="Collapsed"/>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Path=Accounts.IsLoading}" Value="true">
                            <Setter Property="Visibility" Value="Visible"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </local:WaitingUserControl.Style>
        </local:WaitingUserControl>
        <StackPanel Orientation="Horizontal" DockPanel.Dock="Top">
            <StackPanel.Style>
                <!--  Style toggling a visibility on an element depending on the accounts having a load error or not  -->
                <Style TargetType="FrameworkElement">
                    <Setter Property="Visibility" Value="Collapsed"/>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Path=Accounts.HasError}" Value="true">
                            <Setter Property="Visibility" Value="Visible"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </StackPanel.Style>
            <Image Source="Images/error.png" Width="24" />
            <Label Style="{StaticResource imageLabel}" Foreground="Red">Corrupted studio file</Label>
        </StackPanel>
        <!--    Tree view of the accounts    -->
        <TreeView DockPanel.Dock="Top" ItemsSource="{Binding Accounts}"
                  SelectedItemChanged="TreeView_SelectedItemChanged">
            <TreeView.Resources>
                <HierarchicalDataTemplate DataType="{x:Type model:AccountViewModel}" ItemsSource="{Binding Artefacts}">
                    <StackPanel Orientation="Horizontal">
                        <Image Source="Images/account.png" />
                        <Label Style="{StaticResource imageLabel}" Content="{Binding Id}" />
                    </StackPanel>
                </HierarchicalDataTemplate>
                <HierarchicalDataTemplate DataType="{x:Type model:FolderViewModel}" ItemsSource="{Binding FolderContent}">
                    <StackPanel Orientation="Horizontal">
                        <Image Source="Images/folder.png" />
                        <Label Style="{StaticResource imageLabel}" Content="{Binding FolderDisplayName}" />
                        <local:WaitingUserControl>
                            <local:WaitingUserControl.Style>
                                <!--  Style toggling a visibility on an element depending on the folder content or not  -->
                                <Style TargetType="FrameworkElement">
                                    <Setter Property="Visibility" Value="Collapsed"/>
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding Path=FolderContent.IsLoading}" Value="true">
                                            <Setter Property="Visibility" Value="Visible"/>
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </local:WaitingUserControl.Style>
                        </local:WaitingUserControl>
                    </StackPanel>
                </HierarchicalDataTemplate>
                <HierarchicalDataTemplate DataType="{x:Type model:DatabaseViewModel}" ItemsSource="{Binding Artefacts}">
                    <StackPanel Orientation="Vertical">
                        <StackPanel Orientation="Horizontal">
                            <Image Source="Images/db.png" />
                            <Label Style="{StaticResource imageLabel}" Content="{Binding Name}" />
                        </StackPanel>
                    </StackPanel>
                </HierarchicalDataTemplate>
                <HierarchicalDataTemplate DataType="{x:Type model:CollectionViewModel}">
                    <StackPanel Orientation="Vertical">
                        <StackPanel Orientation="Horizontal">
                            <Image Source="Images/collection.png" />
                            <Label Style="{StaticResource imageLabel}" Content="{Binding Name}" />
                        </StackPanel>
                    </StackPanel>
                </HierarchicalDataTemplate>
            </TreeView.Resources>
        </TreeView>
    </DockPanel>
</UserControl>
